//
//  TopView.m
//  商城
//
//  Created by runone02 on 2018/9/17.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "TopView.h"
#import "TopViewCell.h"


@interface TopView()<UICollectionViewDelegate,UICollectionViewDataSource>
/** */
@property (nonatomic, strong) UICollectionView *collectionView;
/** */
@property (nonatomic, strong) NSArray *cellArray;
/** 图片数组*/
@property (nonatomic,strong) NSArray *imageArray;
@end
@implementation TopView
static NSString *const collectionCellId = @"TopViewCell";

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        self.cellArray = @[@"arrow_down",@"biaoqian",@"default_50",@"biaoqian",@"default_50"];
        
        self.imageArray = @[@"arrow_down",@"biaoqian",@"default_50",@"biaoqian",@"default_50"];
    }
    return self;
}

- (void)setupUI {
   
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;   //设置滚动方向
    layout.itemSize = CGSizeMake(SCREEN_WIDTH/6.3, 50);   //设置item大小
    layout.minimumLineSpacing = 10 ;    //设置间距
    layout.minimumInteritemSpacing = 10 ;
  
     self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.showsVerticalScrollIndicator = NO;    //设置滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.bounces = YES;
    [self.collectionView registerClass:[TopViewCell class] forCellWithReuseIdentifier:collectionCellId];
    
    [self addSubview:_collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
     return self.cellArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TopViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellId forIndexPath:indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];
    cell.NameLabel.text = self.cellArray[indexPath.row];

    cell.imageView.image = IMAGE(self.imageArray[indexPath.row]);
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了第%ld个",(long)indexPath.row);
    if (indexPath.row == 0) {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TopOne" object:nil];
    }
   
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
